home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / archival / ftp / BFTP.312 / fts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-29  |  5.1 KB  |  182 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *     Background File Transfer Program (BFTP)                *
  4.  *                                    *
  5.  *    Written at USC/Information Sciences Institute            *
  6.  *    September, 1988                            *
  7.  *                                    *
  8.  *      BFTP is Public Domain, and may be used for any purpose as    *
  9.  *      long as this notice is not removed.  USC-ISI does not assume    *
  10.  *    any responsibility for the correctness, performance, or use    *
  11.  *    of this software.                        *
  12.  *                                    *
  13.  ************************************************************************/
  14.  /* 
  15.   *        File Transfer Service (FTS) -- fts.c
  16.   *
  17.   *  The File Transfer Service provides background file transfer service
  18.   *  for the Internet, by driving FTP Servers to cause the desired file
  19.   *  transfer.  This module contains code to actually cause a single file
  20.   *  transfer operation, using the FTP server-server model.  That is, it
  21.   *  opens FTP control connections to two hosts A and B, logs into each 
  22.   *  one and causes a transfer from one to the other.  It is invoked with
  23.   *  the command "fx request-file".
  24.  
  25.   *  The "request-file" consists of the necessary parameters for the file
  26.   *  transfer, in ASCII format, separated by commas.  Such a request file 
  27.   *  may be created by the user interface module, "bftp", or "Background 
  28.   *  File Transfer Program".
  29.   *
  30.   *  For each of the two server hosts, there will be a structure containing
  31.   *  parameters relevant to each server.  The pointer to such a structure,
  32.   *  called the server-handle, will be a parameter to many of the routines
  33.   *  in this module.  Additional parameters, which apply to the file
  34.   *  transfer as a whole, will also be defined at the beginning of this
  35.   *  module.
  36.   *  
  37.   */
  38.  
  39. #include <stdio.h>
  40. #include <errno.h>
  41. #include <ctype.h>
  42.  
  43. #include <sys/time.h>
  44. #include <sys/types.h>
  45. #include <sys/param.h>
  46. #include <netinet/in.h>    
  47.     
  48. #include "fts.h"
  49.  
  50. extern int read_req();
  51. extern int write_req();
  52. extern int queue_req();
  53. extern int finish_req();
  54.  
  55. /*
  56. *    Global Variables
  57. */     
  58. struct server_struct src, dst;
  59.         /* source and destination structures */
  60. struct fileinfo fil;
  61.  
  62. boolean    fts_timed_out = FALSE;
  63.  
  64. extern boolean conference;
  65. extern FILE
  66.      *tracefp,  /* File to save trace of Telnet history */
  67.     *logfp;    /* File to compose message */
  68.     
  69. void giveup()
  70. {
  71.    closeconn(&src);
  72.    if ((fil.reqtype != DFILE) && (fil.reqtype != VERIFY_SRC))
  73.       closeconn(&dst);
  74.    fts_timed_out = TRUE;
  75.    fprintf(logfp,"\nFTS server timed out.\n");
  76. }
  77.  
  78. main(argc, argv)
  79.    int argc;
  80.    char **argv;
  81. {
  82.     char temp[100], reqfile[100], listfile[100];
  83.     int retcode, interval, jobno = 0;
  84.     struct reqinfo r;
  85.     char **argp = argv + 1;
  86.     FILE *stream;
  87.     
  88.     tracefp = NULL;
  89.     if (argc < 2) {
  90.         fprintf(stderr, "\n\nUsage: fts [-v] file-name\n");
  91.         exit(0);
  92.     }
  93.     
  94.     while ((argc) && **argp == '-') {
  95.         argc--;
  96.         switch (*(1+*argp)) {
  97.            case 'c': /* send dot-file */
  98.                 conference = TRUE;
  99.                 break;
  100.            case 'v':
  101.             tracefp = stdout;
  102.             break;
  103.  
  104.            default:
  105.             fprintf(stderr,"fts: Unknown keyword %s\n", *argp);
  106.             exit(0);
  107.            }
  108.         argp++;
  109.      }
  110.     
  111.     strcpy(reqfile,*argp);
  112.     if (! read_req(reqfile, &r, &src.h, &dst.h, &fil, listfile)) {
  113.        fprintf(stderr,
  114.               "\n\nfts: Request file \'%s\' not found.\n",reqfile);
  115.        return;
  116.        }
  117.  
  118.     if (strlen(r.mailbox)==0 || strlen(r.mailfile)==0 || 
  119.         (logfp = fopen(r.mailfile,"a")) == NULL)
  120.        logfp = stdout;
  121.     if (tracefp) 
  122.        tracefp = logfp;
  123.  
  124.     format_time(0, temp);
  125.     fprintf(logfp,"\n  %s: starting...\n\n", temp);
  126.     
  127.     /* submit extra request, just in case */
  128.     if (r.ntries) r.ntries--;
  129.     if (strlen(r.cmdfile)) {
  130.        queue_req(&r, (time(0)+MAXFTSTIME+60), temp);
  131.        jobno = find_job(reqfile);
  132.        }
  133.     print_req(logfp, &r, &src.h, &dst.h, &fil, FALSE);
  134.     
  135.     signal(SIGALRM, giveup); 
  136.     fts_timed_out = FALSE;
  137.     alarm(MAXFTSTIME); /* start a timer */
  138.     retcode = xfer( &src, (fil.reqtype == DFILE) ? NULL:
  139.                 (fil.reqtype==VERIFY_SRC) ? NULL:
  140.                 &dst, &fil, reqfile, listfile);
  141.     alarm(0); /* cancel the timer */
  142.  
  143.     format_time(0, temp);
  144.     fprintf(logfp,"\n  %s: completed %ssuccessfully.\n\n", temp, 
  145.             (retcode == OK)?"":"un");
  146.     fflush(logfp);
  147.     
  148.     if ((strlen(r.cmdfile) != 0) &&
  149.         ((retcode == ERR_RETRY) || fts_timed_out) && 
  150.         (r.ntries > 0)) {
  151.        /* rewrite request file and resubmit request */
  152.        interval = r.interval;
  153.            if (r.interval < MAXINTERVAL) {
  154.           r.interval = r.interval*2;
  155.           if (r.interval > MAXINTERVAL)
  156.              r.interval = MAXINTERVAL;
  157.           } /* *** use MIN? */
  158.        
  159.        write_req(reqfile, &r, &src.h, &dst.h, &fil, 
  160.                (fil.multflag && (fil.reqtype == COPY))? listfile:"");
  161.        queue_req(&r, (time(0)+(60*interval)), temp);
  162.        fprintf(logfp,"%s\n",temp);
  163.        fflush(logfp);
  164.        /* cancel the extra request */
  165.        cancel_job(jobno);
  166.        if (logfp && (logfp != stdout)) fclose(logfp);
  167.        }
  168.     else {
  169.        /* cancel the extra request */
  170.        cancel_job(jobno);
  171.        if (logfp && (logfp != stdout)) fclose(logfp);
  172.        
  173.        /* send the results message and delete the request files */
  174.        strcpy(temp, src.h.file);
  175.        strcat(temp, ((retcode == OK) && fil.multflag)? " -- Completed" :
  176.                (retcode == OK)? " -- Succeeded" : 
  177.                     " -- Failed");
  178.        finish_req(reqfile, &r, temp, listfile);
  179.        }
  180.        
  181. } /* main */
  182.